import { getToken, setToken, removeToken } from '@/utils/auth'
import { login, userInfoDateilRequest, userInfoRequest } from '@/api/user'
import { resetRouter } from '@/router'
export default {
  namespaced: true,
  state: {
    token: getToken() || '',
    userInfo: {}
  },
  mutations: {
    setToken(state, token) {
      state.token = token
      // 获取token之后存到vuex里面
      setToken(token)
    },
    removeToken(state) {
      state.token = ''
      removeToken()
    },
    setUserInfo(state, payload) {
      state.userInfo = payload
    },
    removeInfo(state) {
      state.userInfo = {}
    }
  },
  actions: {
    async loginAction(context, payload) {
      const res = await login(payload)
      context.commit('setToken', res.data)
    },
    async getUserInofAction(context) {
      try {
        const res = await userInfoRequest()
        console.log(res, '维维')

        const info = await userInfoDateilRequest(res.data.userId)
        // console.log(info)
        context.commit('setUserInfo', { ...res.data, ...info.data })
        return res.data.roles.menus
      } catch (err) {
        console.log('获取接口错误', err)
      }
    },
    logout(context) {
      context.commit('removeToken')
      context.commit('removeInfo')
      // 为了消除重复定义的提示就需要在退出的时候进行路由重置
      resetRouter()
    }

  }
}
